package org.jeecg.modules.exKaibiao.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.exKaibiao.entity.ExBidOpeningRecord;
import org.jeecg.modules.exKaibiao.mapper.ExBidOpeningRecordMapper;
import org.jeecg.modules.exKaibiao.service.IExBidOpeningRecordService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 开标记录
 * @Author: jeecg-boot
 * @Date: 2024-12-17
 * @Version: V1.0
 */
@Service
public class ExBidOpeningRecordServiceImpl extends ServiceImpl<ExBidOpeningRecordMapper, ExBidOpeningRecord> implements IExBidOpeningRecordService {

    @Override
    public ExBidOpeningRecord getBySectionId(String sectionId) {
        LambdaQueryWrapper<ExBidOpeningRecord> query = new LambdaQueryWrapper<ExBidOpeningRecord>().like(ExBidOpeningRecord::getBidSectionId, sectionId);
        return this.getOne(query);
    }

    @Override
    public void syncRecordStatus(List<String> sectionIds) {
        //查询状态为进行中的评标记录，如果已到截止时间 未完成的，开标记录状态置为 3 作废
        LambdaQueryWrapper<ExBidOpeningRecord> query = new LambdaQueryWrapper<ExBidOpeningRecord>().eq(ExBidOpeningRecord::getStatus, "1").in(ExBidOpeningRecord::getBidSectionId, sectionIds);
        List<ExBidOpeningRecord> list = this.list(query);
        list.forEach(item -> {
            //开标截止时间 + 1分钟（避免和 完成开标 冲突）
            Date decryptEndTime = DateUtil.offsetMinute(item.getDecryptEndTime(), 1);
            int compareResult = DateUtil.compare(new Date(), decryptEndTime);
            if (compareResult > 0) {
                item.setStatus("3");//作废
                item.setUpdateTime(new Date());
            }
        });
        this.updateBatchById(list);
    }
}
